using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;

namespace Sedna.Core.Controls.Support
{
	public class LayoutCell : ILayoutCell
	{

		private string configurationId = "configuration.default.LayoutCell";
		private ILayoutCellConfiguration configuration;

		IList controls;
		HorizontalAlign align = HorizontalAlign.NotSet;
		VerticalAlign valign = VerticalAlign.NotSet;
		int rowSpan = 1;
		int colSpan = 1;
		string cssClass = null;
		private Unit width = 0;
		private Unit height;
		bool noWrap = false;

		public LayoutCell()
		{
			controls = new ArrayList();
		}

		public IConfiguration Configuration
		{
			get{return configuration;}
			set{this.configuration = (ILayoutCellConfiguration)value;}
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.LayoutCell")]
		public string ConfigurationId
		{
			get{return configurationId;}
			set{this.configurationId = value;}
		}

		public HorizontalAlign Align
		{
			get{return align;}
			set{align = value;}
		}

		public VerticalAlign Valign
		{
			get{return valign;}
			set{valign = value;}
		}
		
		public Unit Width
		{
			get{return width;}
			set{width = value;}
		}

		public Unit Height
		{
			get { return height; }
			set { height = value; }
		}

		public int RowSpan
		{
			get{return rowSpan;}
			set{rowSpan = value;}
		}

		public int ColSpan
		{
			get{return colSpan;}
			set{colSpan = value;}
		}

		public string CssClass
		{
			get{return this.cssClass;}
			set{this.cssClass = value;}
		}

		public bool NoWrap
		{
			get{return this.noWrap;}
			set{this.noWrap = value;}
		}

		public IList Controls
		{
			get { return controls; }
			set { controls = value; }
		}

		public static LayoutCell BuildCell(ILayoutCellConfiguration configuration)
		{
			LayoutCell layoutCell = new LayoutCell();
			layoutCell.align = configuration.Align;
			layoutCell.colSpan = configuration.ColSpan;
			if (configuration.Controls != null) 
			{
				for (int i = 0; i < configuration.Controls.Count; i++)
				{
					layoutCell.Controls.Add(ControlBuilder.BuildControl((IConfiguration) configuration.Controls[i]));
				}
			}
			layoutCell.cssClass = configuration.CssClass;
			layoutCell.noWrap = configuration.NoWrap;
			layoutCell.rowSpan = configuration.RowSpan;
			layoutCell.valign = configuration.Valign;
			layoutCell.width = configuration.Width;
			layoutCell.height = configuration.Height;

			return layoutCell;
		}

	}
}
